<?php

/**
 * Description of Dispatcher
 *
 * @author miho
 */
class BluesCoders_Event_Dispatcher {

    protected static $_instance;

    /**
     *
     * @return BluesCoders_Event_Dispatcher 
     */
    public static function getIsntance() {
        if( self::$_instance == null) {
            self::$_instance = new self();
        }

        return self::$_instance;
    }

    protected $_listeners;

    public function addEventListener($type, BluesCoders_Event_Listener $listener) {
        if(! isset($this->_listeners[$type])) {
            $this->_listeners[$type] = array();
        }
        $this->_listeners[$type][] = $listener;
    }

    public function dispatchEvent(BluesCoders_Event $event) {
        if( !isset($this->_listeners[$event->getType()])) {
            return true;
        }

        foreach($this->_listeners[$event->getType()] as $listener) {
            if( ($result = $listener->listen($event)) === false) {
                return false;
            }
        }
        
        return true;
    }

    

}
?>
